package com.foreknow.demo14;

import java.lang.reflect.Parameter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author shkstart
 * @create 2021-08-03 13:11
 * ：主要用于执行动态的sql
 * 预处理
 */
public class PrepareStatementDemo {
    public static void main(String[] args) {

        //1.加载驱动  jdbc驱动类  com.mysql.jdbc.Driver  8.0改成 com.mysql.cj.jdbc.Driver
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/basic5?useSSL=false&serverTimezone=UTC";

            Connection conn = DriverManager.getConnection(url, "root", "123456");

            //3.获取PreparedStatement对象用于执行动态SQL
            PreparedStatement pstmt = conn.prepareStatement("insert  into guestbook(name ,phone,email,title,content) values(?,?,?,?,?)");
            pstmt.setString(1,"fender");
            pstmt.setString(2,"13458761");
            pstmt.setString(3,"fender@163.com");
            pstmt.setString(4,"title");
            pstmt.setString(5,"test!!!");
            int i = pstmt.executeUpdate();
            System.out.println(i);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
